<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use App\Purchase as Purchase;
use App\PurchaseItem as PurchaseItem;
use App\Http\Requests\PurchaseRequest;
class PurchaseController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $purchase=Purchase::AllOrSearch(Input::get('q'));
        return response()->view("admin.purchase.index",  compact('purchase'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return response()->view('admin.purchase.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(PurchaseRequest $request)
    {
        $data = $request->all();
        $all_price=0;
        $purchase_nums=0;
        foreach($data["goods"] as $k=>$v){
            $purchase_nums+=intval($v["purchase_nums"]);
            $all_price+=intval($v["purchase_nums"])*$v["purchase_price"];
            
        }
        $purchase=array();
        $purchase["codeno"]=$data["codeno"];
        $purchase["company"]=$data["company"];
        $purchase["created_at"]=$data["created_at"];
        $purchase["purchase_nums"]=$purchase_nums;
        $purchase["all_price"]=$all_price;
        
        if(isset($data["remark"])){
            $purchase["remark"]=$data["remark"];
        }
            
            $model=Purchase::create($purchase);
            foreach($data["goods"] as $k2=>$v2){
                $purchase_item=array();
                $purchase_item["purchase_id"]=$model->id;
                $purchase_item["goods_id"]=$k2;
                $purchase_item["purchase_nums"]=intval($v2["purchase_nums"]);
                $purchase_item["purchase_price"]=$v2["purchase_price"];
                PurchaseItem::create($purchase_item);
            }
    
        
        return redirect()->route('admin.purchase.index');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
            try{
                $purchase=  Purchase::findOrFail($id);
                return response()->view('admin.purchase.edit', compact('purchase'));
            }catch (ModelNotFoundException $e) {
                return redirect()->route('admin.nofound');
            }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(PurchaseRequest $request, $id)
    {
        try{
         $purchase_obj=  Purchase::findOrFail($id);
         PurchaseItem::where('purchase_id', $id)->delete();
         $data = $request->all();
        $all_price=0;
        $purchase_nums=0;
        foreach($data["goods"] as $k=>$v){
            $purchase_nums+=intval($v["purchase_nums"]);
            $all_price+=intval($v["purchase_nums"])*$v["purchase_price"];
            
        }
        $purchase=array();
        $purchase["codeno"]=$data["codeno"];
        $purchase["company"]=$data["company"];
        $purchase["created_at"]=$data["created_at"];
        $purchase["purchase_nums"]=$purchase_nums;
        $purchase["all_price"]=$all_price;
        
        if(isset($data["remark"])){
            $purchase["remark"]=$data["remark"];
        }
            
            $purchase_obj->update($purchase);
            foreach($data["goods"] as $k2=>$v2){
                $purchase_item=array();
                $purchase_item["purchase_id"]=$id;
                $purchase_item["goods_id"]=$k2;
                $purchase_item["purchase_nums"]=intval($v2["purchase_nums"]);
                $purchase_item["purchase_price"]=$v2["purchase_price"];
                PurchaseItem::create($purchase_item);
            }
            return redirect()->route('admin.purchase.index');
        }catch (ModelNotFoundException $e){
            return redirect()->route('nofound');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        try{
            $purchase_obj=  Purchase::findOrFail($id);
            $purchase_obj->update(array("status"=>0));
            return redirect()->route('admin.purchase.index');
         }catch (ModelNotFoundException $e){
            return redirect()->route('nofound');
        }
    }
}
